package com.internship.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.internship.model.DepartmentInfo;
import com.internship.model.JobStudent;
import com.internship.model.Question;
import com.internship.model.QuestionReply;
import com.internship.model.TeacherInfo;
import com.internship.page.Page;
import com.internship.service.Const;
import com.internship.service.DepartmentInfoService;
import com.internship.service.JobStudentService;
import com.internship.service.QuestionService;
import com.internship.service.TeacherInfoService;
import com.internship.util.StringUtil;


@Controller
@RequestMapping("question")
public class QuestionController {
	private final Logger logger = Logger.getLogger(QuestionController.class);
	
	@Autowired
	private QuestionService questionService;
	@Autowired
	private DepartmentInfoService 	departmentInfoService;
	@Autowired
	private TeacherInfoService teacherInfoService;
	@Autowired
	private JobStudentService 	jobStudentService;
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}
	/**?
	 * 实习问题列表
	 * @return
	 */
	@RequestMapping("/question_list.do")
	public ModelAndView getQuestionList() {
		return getQuestionList(null,null,1);
	}
	
	private List<DepartmentInfo>  departmentInfoList = null;
	@RequestMapping("/question_page_list.do")
	public ModelAndView getQuestionList(@RequestParam String StudentName,@RequestParam String DeptCode,@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("question");
		HttpSession session = getSession();
		try {
			if (String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("2")||String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("1")) {
				Page<Question> pageList = questionService.getQuestionList(StudentName, DeptCode, null, pageNumber);
				this.departmentInfoList = departmentInfoService.getDepartmentInfoList();
				mv.addObject("currentPage", pageNumber);
				mv.addObject("DeptCode", DeptCode);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("departmentInfoList",departmentInfoList);
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("questionList", pageList.getResultList());
			}else if(String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("3")){
				Page<Question> pageList = questionService.getQuestionList(StudentName,String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)),null, pageNumber);
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("questionList", pageList.getResultList());
			}else {
				Page<Question> pageList = questionService.getQuestionList(StudentName,String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)),String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)), pageNumber);
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("questionList", pageList.getResultList());
			}
			mv.addObject("StudentName", StudentName);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return mv;
	}
	/**?
	 * 我的实习问题列表
	 * @return
	 */
	@RequestMapping("/myQuestion_list.do")
	public ModelAndView getMyQuestionList() {
		return getMyQuestionList(1);
	}
	@RequestMapping("/myQuestion_page_list.do")
	public ModelAndView getMyQuestionList(@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("my_question");
		HttpSession session = getSession();
		try {
			JobStudent jobStudent =jobStudentService.getJobStudent(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
			if (jobStudent!=null) {
					Page<Question> pageList = questionService.getMyQuestionList(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)), pageNumber);
					mv.addObject("currentPage", pageNumber);
					mv.addObject("totalPage", pageList.getTotalPage());
					mv.addObject("totalCount", pageList.getTotalCount());
					mv.addObject("questionList", pageList.getResultList());
				}else {
						mv.addObject("msg", " 您可能还未参加实习，无法填写问题!");
						 mv.setViewName("error");
					}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return mv;
	}
	/**?
	 * 问题统计
	 * @return
	 */
	@RequestMapping("/question_statistics_list.do")
	public ModelAndView questionStatistics() {
		return questionStatistics(null,0);
	}
	@RequestMapping("/question_statistics_page_list.do")
	public ModelAndView questionStatistics(@RequestParam String DeptCode,@RequestParam int grade) {
		ModelAndView mv = new ModelAndView("question_statistics");
		HttpSession session = getSession();
		try {
			if (String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("2")||String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("1")) {
					int questionCount = questionService.getQuestionCount(DeptCode, grade);
					int questionNotesCount = questionService.getQuestionUnreadCount(DeptCode, grade);
					mv.addObject("questionCount",questionCount);
					mv.addObject("questionNotesCount",questionNotesCount);
					mv.addObject("DeptCode",DeptCode);
					this.departmentInfoList = departmentInfoService.getDepartmentInfoList();
					mv.addObject("departmentInfoList",departmentInfoList);
			}else {
				int questionCount = questionService.getQuestionCount(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)), grade);
				int questionNotesCount = questionService.getQuestionUnreadCount(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)), grade);
				mv.addObject("questionCount",questionCount);
				mv.addObject("questionNotesCount",questionNotesCount);
				}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		String[] gradeList = StringUtil.getCurrentGrades();
		mv.addObject("gradeList", gradeList);
		mv.addObject("grade", grade);
		return mv;
	}
	@RequestMapping("/question_reply_list.do")
	public ModelAndView questionReply(@RequestParam int QuestionId) {
		ModelAndView mv = new ModelAndView("question_reply");
		Question question =questionService.getQuestionById(QuestionId);
		List<QuestionReply> list = questionService.getQuestionReplyList(QuestionId);
		int questionReplyCount =questionService.getQuestionReplyCount(QuestionId);
		mv.addObject("questionReplyList", list);
		mv.addObject("questionReplyCount", questionReplyCount);
		mv.addObject("question", question);
 		return mv;
	}
	@RequestMapping("/add_reply.do")
	@ResponseBody
	public JSONObject addReply(@RequestParam("QuestionId")int QuestionId , @RequestParam("ReplyContent")String ReplyContent) {
		JSONObject json = new JSONObject();
		HttpSession session = getSession();
		try {
		QuestionReply questionReply = new QuestionReply();
		questionReply.setQuestionId(QuestionId);
		questionReply.setReplyContent(ReplyContent);
		questionReply.setReplyTime(StringUtil.getStrDateTime());
		questionReply.setTeacherNumber(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
		questionReply.setTeacherName(String.valueOf(session.getAttribute(Const.USER_NAME)));
		int rel = questionService.addQuestionReply(questionReply);
		if (rel>0) {
			List<QuestionReply> list = questionService.getQuestionReplyList(QuestionId);
			json.put("questionReplyList", list);
			Question question = questionService.getQuestionById(QuestionId);
			question.setHasReply(1);
			questionService.updateQuestion(question);
			System.out.println(question.getHasReply()+"============");
		}else {
				json.put("questionReplyList", "error");
		}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return json;
	}
	/**
	 * 指导老师指导学生的问题列表
	 * @param ZGH
	 * @return
	 */
	@RequestMapping("/guide_student_question_list.do")
	public ModelAndView getGuideStudentQuestionList(@RequestParam("ZGH")String  ZGH) {
		return getGuideStudentQuestionList(ZGH,1);
	}
 @RequestMapping("/guide_student_question_page_list.do")
	public ModelAndView getGuideStudentQuestionList(@RequestParam String ZGH,@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("guide_student_question_list");
		TeacherInfo teacherInfo = teacherInfoService.geTeacherInfoByZGH(ZGH);
		Page<Question> pageList = questionService.getTeacherGuideQuestionList(ZGH, pageNumber);
		mv.addObject("currentPage", pageNumber);
		mv.addObject("totalPage", pageList.getTotalPage());
		mv.addObject("totalCount", pageList.getTotalCount());
		mv.addObject("guideStudentQuestionList", pageList.getResultList());
		mv.addObject("teacherInfo",teacherInfo);
		return mv;
	}
 /**?
  * 问题添加
  * @return
  */
 @RequestMapping("/question_add.do")
	public ModelAndView questionAdd() {
		HttpSession session = getSession();
		ModelAndView mv = new ModelAndView("my_question_add");
		JobStudent jobStudent = jobStudentService.getJobStudent(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
		mv.addObject("jobStudent", jobStudent);
		return mv;
	}
 @RequestMapping("/question_save.do")
	@ResponseBody
	public JSONObject questionSave(@RequestParam("Question")String Question  ) {
		JSONObject json = new JSONObject();
		HttpSession session = getSession();
		JobStudent jobStudent = jobStudentService.getJobStudent(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
		Question question1 = new Question();
		question1.setDeptCode(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)));
		question1.setGrade(jobStudent.getDQSZJ());
		question1.setHasReply(0);
		question1.setJobStudentNumber(jobStudent.getXH());
		question1.setQuestion(Question);
		question1.setSendTime(StringUtil.getStrDateTime());
		question1.setStudentName(jobStudent.getXM());
			question1.setTeacherName(jobStudent.getSchTeacherName());
			question1.setTeacherNumber(jobStudent.getSchoolTeacher());
		int rel = questionService.addQuestion(question1);
		json.put("result", rel>0?"success":"error");
		return json;
	}
 /**?
	 * 删除
	 * @param Id
	 * @return
	 */
		@RequestMapping("/delete.do")
		@ResponseBody
		public JSONObject delNews(@RequestParam int id){
			JSONObject json = new JSONObject();
			int  rel= questionService.delQuestion(id);
			json.put("result", rel > 0 ?"success":"error");
			 return json; 
		}
		/**?
		 * 我的实习问题列表 手机端
		 * @return
		 */
		@RequestMapping("/moible_myQuestion_list.do")
		public ModelAndView getMoibleMyQuestionList() {
			return getMoibleMyQuestionList(1);
		}
		@RequestMapping("/moible_myQuestion_page_list.do")
		public ModelAndView getMoibleMyQuestionList(@RequestParam int pageNumber) {
			ModelAndView mv = new ModelAndView("moible_my_question");
			HttpSession session = getSession();
			try {
				JobStudent jobStudent =jobStudentService.getJobStudent(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
				if (jobStudent!=null) {
						Page<Question> pageList = questionService.getMyQuestionList(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)), pageNumber);
						mv.addObject("currentPage", pageNumber);
						mv.addObject("totalPage", pageList.getTotalPage());
						mv.addObject("totalCount", pageList.getTotalCount());
						mv.addObject("questionList", pageList.getResultList());
					}else {
							mv.addObject("msg", " 您可能还未参加实习，无法填写问题!");
							 mv.setViewName("error");
						}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return mv;
		}
		/**?
		  * 问题添加 手机端
		  * @return
		  */
		 @RequestMapping("/moible_question_add.do")
			public ModelAndView moibleQuestionAdd() {
				HttpSession session = getSession();
				ModelAndView mv = new ModelAndView("moible_my_question_add");
				JobStudent jobStudent = jobStudentService.getJobStudent(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
				mv.addObject("jobStudent", jobStudent);
				return mv;
			}
		 /**?
		  * 问题查看 手机端
		  * @param QuestionId
		  * @return
		  */
		 @RequestMapping("/moible_question_reply_list.do")
			public ModelAndView moibleQuestionReply(@RequestParam int QuestionId) {
				ModelAndView mv = new ModelAndView("moible_question_reply");
				Question question =questionService.getQuestionById(QuestionId);
				List<QuestionReply> list = questionService.getQuestionReplyList(QuestionId);
				int questionReplyCount =questionService.getQuestionReplyCount(QuestionId);
				mv.addObject("questionReplyList", list);
				mv.addObject("questionReplyCount", questionReplyCount);
				mv.addObject("question", question);
		 		return mv;
			}
}